﻿using System;
using System.Collections.Generic;

using GalaSoft.MvvmLight.Command;

using Rio.Framework.Localisation;
using Rio.Framework.Pages.AppCode.Models;
using Rio.Manager.Application.ViewModel.BaseClass;
using Rio.Manager.Application.ViewModel.Locator;

namespace Rio.Manager.Application.ViewModel.GeneralTabs
{
    public class FolderTypeAdministrationTabViewModel : TabViewModelBase
    {
        #region Fields

        private string _folderTypeLabel;
        private string _itemsPlanCategoryDescription;
        private Dictionary<string, string> _itemsPlanCategoryItems;
        private string _itemsPlanCategoryTitle;
        private string _organizationLabel;
        private string _rightsCategoryDescription;
        private Dictionary<string, string> _rightsCategoryItems;
        private string _rightsCategoryTitle;
        private string _versionManageCategoryDescription;
        private List<ObservableKeyValuePair<string, string>> _versionManageCategoryItems;
        private ObservableKeyValuePair<string, string> _versionManageCategorySelectedItem;
        private string _versionManageCategoryTitle;
        private string _workflowCategoryDescription;
        private List<ObservableKeyValuePair<string, string>> _workflowCategoryItems;
        private ObservableKeyValuePair<string, string> _workflowCategorySelectedItem;
        private string _workflowCategoryTitle;

        #endregion Fields

        #region Constructors

        public FolderTypeAdministrationTabViewModel()
            : base()
        {
            TabIcon = new Uri("/Rio.Manager.Application;component/Assets/Images/administration_folderType.png", UriKind.Relative);

            if (IsInDesignMode)
            {
                InitLabels();
                InitCategories();
            }
        }

        #endregion Constructors

        #region Properties

        public string FolderTypeLabel
        {
            get { return _folderTypeLabel; }
            set
            {
                _folderTypeLabel = value;
                RaisePropertyChanged("FolderTypeLabel");
            }
        }

        public RelayCommand InitializationCommand
        {
            get;
            set;
        }

        public string ItemsPlanCategoryDescription
        {
            get { return _itemsPlanCategoryDescription; }
            set
            {
                _itemsPlanCategoryDescription = value;
                RaisePropertyChanged("ItemsPlanCategoryDescription");
            }
        }

        public Dictionary<string, string> ItemsPlanCategoryItems
        {
            get { return _itemsPlanCategoryItems; }
            set
            {
                _itemsPlanCategoryItems = value;
                RaisePropertyChanged("ItemsPlanCategoryItems");
            }
        }

        public string ItemsPlanCategoryTitle
        {
            get { return _itemsPlanCategoryTitle; }
            set
            {
                _itemsPlanCategoryTitle = value;
                RaisePropertyChanged("ItemsPlanCategoryTitle");
            }
        }

        public RelayCommand<string> OpenNewTabCommand
        {
            get;
            set;
        }

        public string OrganizationLabel
        {
            get { return _organizationLabel; }
            set
            {
                _organizationLabel = value;
                RaisePropertyChanged("OrganizationLabel");
            }
        }

        public string RightsCategoryDescription
        {
            get { return _rightsCategoryDescription; }
            set
            {
                _rightsCategoryDescription = value;
                RaisePropertyChanged("RightsCategoryDescription");
            }
        }

        public Dictionary<string, string> RightsCategoryItems
        {
            get { return _rightsCategoryItems; }
            set
            {
                _rightsCategoryItems = value;
                RaisePropertyChanged("RightsCategoryItems");
            }
        }

        public string RightsCategoryTitle
        {
            get { return _rightsCategoryTitle; }
            set
            {
                _rightsCategoryTitle = value;
                RaisePropertyChanged("RightsCategoryTitle");
            }
        }

        public string VersionManageCategoryDescription
        {
            get { return _versionManageCategoryDescription; }
            set
            {
                _versionManageCategoryDescription = value;
                RaisePropertyChanged("VersionManageCategoryDescription");
            }
        }

        public List<ObservableKeyValuePair<string, string>> VersionManageCategoryItems
        {
            get { return _versionManageCategoryItems; }
            set
            {
                _versionManageCategoryItems = value;
                RaisePropertyChanged("VersionManageCategoryItems");
            }
        }

        public ObservableKeyValuePair<string, string> VersionManageCategorySelectedItem
        {
            get { return _versionManageCategorySelectedItem; }
            set
            {
                _versionManageCategorySelectedItem = value;
                RaisePropertyChanged("VersionManageCategorySelectedItem");
            }
        }

        public string VersionManageCategoryTitle
        {
            get { return _versionManageCategoryTitle; }
            set
            {
                _versionManageCategoryTitle = value;
                RaisePropertyChanged("VersionManageCategoryTitle");
            }
        }

        public string WorkflowCategoryDescription
        {
            get { return _workflowCategoryDescription; }
            set
            {
                _workflowCategoryDescription = value;
                RaisePropertyChanged("WorkflowCategoryDescription");
            }
        }

        public List<ObservableKeyValuePair<string, string>> WorkflowCategoryItems
        {
            get { return _workflowCategoryItems; }
            set
            {
                _workflowCategoryItems = value;
                RaisePropertyChanged("WorkflowCategoryItems");
            }
        }

        public ObservableKeyValuePair<string, string> WorkflowCategorySelectedItem
        {
            get { return _workflowCategorySelectedItem; }
            set
            {
                _workflowCategorySelectedItem = value;
                RaisePropertyChanged("WorkflowCategorySelectedItem");
            }
        }

        public string WorkflowCategoryTitle
        {
            get { return _workflowCategoryTitle; }
            set
            {
                _workflowCategoryTitle = value;
                RaisePropertyChanged("WorkflowCategoryTitle");
            }
        }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Inits the commands.
        /// </summary>
        public override void InitCommands()
        {
            InitializationCommand = new RelayCommand(Initialization);
            OpenNewTabCommand = new RelayCommand<string>(OpenNewTab);
        }

        /// <summary>
        /// Initializations this instance.
        /// </summary>
        public void Initialization()
        {
            InitLabels();
            InitCategories();
        }

        /// <summary>
        /// Inits the labels.
        /// </summary>
        public override void InitLabels()
        {
            ItemsPlanCategoryTitle = Resx.GetLabel(ResxRioManager.ITEMS_PLAN_CATEGORY_TITLE);
            ItemsPlanCategoryDescription = Resx.GetLabel(ResxRioManager.ITEMS_PLAN_CATEGORY_DESCRIPTION);

            RightsCategoryTitle = Resx.GetLabel(ResxRioManager.RIGHTS_CATEGORY_TITLE);
            RightsCategoryDescription = Resx.GetLabel(ResxRioManager.RIGHTS_CATEGORY_DESCRIPTION);

            VersionManageCategoryTitle = Resx.GetLabel(ResxRioManager.VERSION_MANAGE_CATEGORY_TITLE);
            VersionManageCategoryDescription = Resx.GetLabel(ResxRioManager.VERSION_MANAGE_CATEGORY_DESCRIPTION);

            WorkflowCategoryTitle = Resx.GetLabel(ResxRioManager.WORKFLOW_CATEGORY_TITLE);
            WorkflowCategoryDescription = Resx.GetLabel(ResxRioManager.WORKFLOW_CATEGORY_DESCRIPTION);

            OrganizationLabel = Resx.GetLabel(ResxCommon.ORGANIZATION);
            FolderTypeLabel = Resx.GetLabel(ResxCommon.FOLDER_TYPE);
        }

        /// <summary>
        /// Opens the new tab.
        /// </summary>
        public void OpenNewTab(string collectionSource)
        {
            TabViewModelBase newTab = null;

            switch (collectionSource)
            {
                case "VersionManageCategory":
                    if (VersionManageCategorySelectedItem != null)
                    {
                        switch (VersionManageCategorySelectedItem.Key)
                        {
                            case "VersioningModel":
                                newTab = ViewModelLocator.VersioningModelTabStatic;
                                break;
                            case "ListUndoCheckOutModel":
                                newTab = ViewModelLocator.ListUndoCheckOutTabStatic;
                                break;
                            default:
                                break;
                        }

                        VersionManageCategorySelectedItem = null;
                    }
                    break;
                case "WorkflowCategory":
                    if (WorkflowCategorySelectedItem != null)
                    {
                        switch (WorkflowCategorySelectedItem.Key)
                        {
                            case "NewWorkflow":
                                newTab = ViewModelLocator.NewWorkflowTabViewModelStatic;
                                break;
                            case "ManageWorkflow":
                                newTab = ViewModelLocator.ManageWorkflowTabViewModelStatic;
                                break;
                            case "ManageWorkflowModel":
                                newTab = ViewModelLocator.ManageWorkflowModelTabViewModelStatic;
                                break;
                            default:
                                break;
                        }

                        WorkflowCategorySelectedItem = null;
                    }
                    break;
                default:
                    break;
            }

            if (newTab != null)
            {
                Action<TabViewModelBase> actionToProcess = (tab) =>
                    {
                        tab.SelectedOrganization = this.SelectedOrganization;
                        tab.SelectedFolderType = this.SelectedFolderType;
                    };

                AddTab(newTab, actionToProcess);
            }
        }

        /// <summary>
        /// Inits the categories.
        /// </summary>
        private void InitCategories()
        {
            /*
             * Each collection of categories is a List<ObservableKeyValuePair<string, string>> where :
             *      - key is a string used in OpenNewTab method to know what tab open
             *      - value is the label localized which will be display
             */

            /*
             * Versions Management Category
             */

            VersionManageCategoryItems = new List<ObservableKeyValuePair<string, string>>();
            VersionManageCategoryItems.Add(new ObservableKeyValuePair<string, string>("VersioningModel", Resx.GetLabel(ResxRioManager.VERSIONING_MODEL)));
            VersionManageCategoryItems.Add(new ObservableKeyValuePair<string, string>("ListUndoCheckOutModel", Resx.GetLabel(ResxRioManager.LIST_UNDO_CHECK_OUT_MODEL)));

            RaisePropertyChanged("VersionManageCategoryItems");

            /*
             * Workflow Category
             */

            WorkflowCategoryItems = new List<ObservableKeyValuePair<string, string>>();
            WorkflowCategoryItems.Add(new ObservableKeyValuePair<string, string>("NewWorkflow", Resx.GetLabel(ResxRioManager.MODELIZE_WORKFLOW)));
            WorkflowCategoryItems.Add(new ObservableKeyValuePair<string, string>("ManageWorkflow", Resx.GetLabel(ResxRioManager.MANAGE_WORFKLOW)));
            WorkflowCategoryItems.Add(new ObservableKeyValuePair<string, string>("ManageWorkflowModel", "Gérer les modèles de workflow."));

            RaisePropertyChanged("WorkflowCategoryItems");
        }

        #endregion Methods
    }
}